package com.health.userbackstage.web.control;

import com.health.userbackstage.mapper.OrderSettingMapper;
import com.health.userbackstage.pojo.OrderSetting;
import com.health.userbackstage.pojo.result.Result;
import com.health.userbackstage.pojo.result.ResultEnum;
import com.health.userbackstage.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author ASUS
 * @Date 2023/8/22 9:24
 * @Version 1.0
 */
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Autowired
    private OrderSettingService oss;

    @Autowired
    private SimpleDateFormat sdf;


    @RequestMapping("/getleftobj")
    public Result getLeftObj(){
        Date date1 = new Date();
        Date date2 = new Date();

        date1.setDate(-7);
        date2.setDate(date2.getDate()+7);

        String min = sdf.format(date1);
        String max = sdf.format(date2);
        List<OrderSetting> byDateRange = oss.findByDateRange(min, max);
        return Result.success(byDateRange);
    }

    @RequestMapping("/editNumber")
    public Result editNumber(@RequestBody OrderSetting os){
        if(oss.updateNumber(os)){
            return Result.success();
        }
        Result result = Result.error(ResultEnum.FAILED_EDIT);
        result.setMessage("修改失败,更改后的预约数不可小于已预约数");
        return result;

    }

    @RequestMapping("/uploadxls")
    private Result upload(MultipartFile excelFile, HttpServletRequest req) throws IOException {
        String uploadFileName = excelFile.getOriginalFilename();
//        String uuid = UUID.randomUUID().toString().replace("-","");
//        int lastDotIdx = uploadFileName.lastIndexOf(".");
//        String fileSuffix = uploadFileName.substring(lastDotIdx);
//        String fileName = uuid+fileSuffix;
//        File saveFile = new File("D:\\java\\task\\health\\health\\userbackstage\\src\\main\\resources\\upload\\" + uploadFileName);
        File saveFile = new File("D:/upload/" + uploadFileName);
        excelFile.transferTo(saveFile);

        return Result.success(uploadFileName);
    }
}
